import java.util.Arrays;

public class HashBuck {
    static class Node{
        public int key;
        public int val;
        public Node next;

        public Node(int key, int val) {
            this.key = key;
            this.val = val;
        }
    }
    public Node[] array=new Node[10];
    public int usedSize;
    //负载因子
    public static final double DEFAULT_Load_FACTOR=0.75f;

    //放进去一个元素
    public void push(int key,int val){
        int index=key%array.length;
        Node cur=array[index];
        while(cur!=null){
            if(cur.key==key){
                cur.val=val;
                return;
            }
            cur=cur.next;
        }
        //走到这里说明没有找到当前链表中有key这个节点
        Node node=new Node(key, val);
        node.next=array[index];
        array[index]=node;
        usedSize++;
        if(loadFactor()>DEFAULT_Load_FACTOR){
            resize();
        }
    }
    //resize方法是给这扩容
    private void resize(){
        Node[] newArray=new Node[2*array.length];
        for(int i=0;i<array.length;i++){
            Node cur=array[i];
            while(cur!=null){
                int newIndex=cur.key% newArray.length;
                Node curN=cur.next;
                cur.next=newArray[newIndex];
                newArray[newIndex]=cur;
                cur=curN;
            }
        }
        array=newArray;
    }

    private double loadFactor(){
        return usedSize*1.0/array.length;
    }
    //再哈希表中查找口感val
    public int getval(int key){
        int index=key%array.length;
        Node cur=array[index];
        while(cur!=null){
            if(cur.key==key){
                return cur.val;
            }
            cur=cur.next;
        }
        return -1;
    }

}
